package com.hyj.ecp.backstage.goods.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ecp.cust.ClientUserQO;
import com.ecp.goods.GoodsQO;
import com.ecp.goods.GoodsTrait;
import com.ecp.goods.GoodsTraitsQO;
import com.ecp.goods.GoodsTypeQO;
import com.hyj.ecp.backstage.goods.service.IBackGoodsService;
import com.hyj.ecp.backstage.sys.bean.vo.User;
import com.hyj.ecp.common.constant.SysConstant;
import com.hyj.util.GzipUtil;
import com.hyj.util.JsonUtil;

@Controller
@SessionAttributes("user")
@RequestMapping("/backstage/goods/*")
public class BackGoodsAction {

	Log logger = LogFactory.getLog(BackGoodsAction.class);
	@Autowired
    private IBackGoodsService service;
	
	    @RequestMapping(value="togoodstype.do")
	    public String togoodstype(GoodsTypeQO qo,ModelMap map) throws IOException
	    {
	    	logger.info("togoodstype.do");
	    	qo.setPid("-1");
	    	qo.setCurPage(-1);//不要分页查询
	    	Map<String,Object> vo = service.queryGoodsType(qo);
	    	map.put("list", vo.get("infoContent"));
			return "view/backstage/goods/GoodType";
	    	
	    }
	    
	    @ResponseBody
		@RequestMapping(value="goodstype.do")
	    public Object queryGoodType(GoodsTypeQO qo)
	    {
	    	logger.info("goodstype.do  start...");

	    	Map<String,Object> vo = service.queryGoodsType(qo);
	    	return vo;
	    }
	    
	    @RequestMapping(value="toAddtypePage.do")
	    public String toAddtypePage(GoodsTypeQO qo,ModelMap map) throws IOException
	    {
	    	logger.info("toAddtypePage");
	    	qo.setPid("-1");
	    	qo.setCurPage(-1);//不要分页查询
	    	Map<String,Object> vo = service.queryGoodsType(qo);
	    	map.put("list", vo.get("infoContent"));
			return "view/backstage/goods/AddgoodType";
	    	
	    }
	    
	    @ResponseBody
	    @RequestMapping(value="addtypePage.do")
	    public Object addtypePage(GoodsTypeQO qo,ModelMap map) throws IOException
	    {
	    	logger.info("addtypePage.do");
	    	User u =  (User)map.get("user");
            Map<String,Object> vo = new HashMap<String,Object>();
            try{
            qo.setOptUser(u.getAccount());
	    	int a = service.saveGoodsType(qo);
	    	if(a>0){
	    		vo.put("returnCode", 0);
	    		vo.put("saveNum", a);
	    		vo.put("msg", "保存成功！");
	    	}else{
	    		vo.put("returnCode", 999);
	    		vo.put("saveNum", a);
	    		vo.put("msg", "保存失败！");
	    	}
            }catch(Exception e){
            	logger.error("addtypePage.do",e);
            	vo.put("returnCode", 999);
	    		vo.put("saveNum", 0);
	    		vo.put("msg", "保存失败！");
            }
	    	return vo;
	    	
	    }
	    
	    @RequestMapping(value="toUpdatetypePage.do")
	    public String toUpdatetypePage(GoodsTypeQO qo,ModelMap map) throws IOException
	    {
	    	logger.info("toUpdatetypePage.do");
	    	
	    	qo.setCurPage(-1);//不要分页查询
	    	Map<String,Object> vo = service.queryGoodsType(qo);
	    	map.put("vo", ((List<?>)vo.get("infoContent")).get(0));
	    	
	    	
	    	// 查询  选择框数据
	    	qo.setId(null);
	    	qo.setPid("-1");
	    	qo.setCurPage(-1);//不要分页查询
	    	Map<String,Object> vo2 = service.queryGoodsType(qo);
	    	map.put("list", vo2.get("infoContent"));
	    	
	    	
			return "view/backstage/goods/UpdategoodType";
	    	
	    }
	    
	    @ResponseBody
	    @RequestMapping(value="updatetypePage.do")
	    public Object updatetypePage(GoodsTypeQO qo,ModelMap map) throws IOException
	    {
	    	logger.info("updatetypePage.do");
	    	User u =  (User)map.get("user");
            Map<String,Object> vo = new HashMap<String,Object>();
            try{
            qo.setOptUser(u.getAccount());
	    	int a = service.updateGoodsType(qo);
	    	if(a>0){
	    		vo.put("returnCode", 0);
	    		vo.put("saveNum", a);
	    		vo.put("msg", SysConstant.UPDATE_SUCCESS_STR);
	    	}else{
	    		vo.put("returnCode", 999);
	    		vo.put("saveNum", a);
	    		vo.put("msg", SysConstant.UPDATE_FAIL_STR);
	    	}
            }catch(Exception e){
            	logger.error("addtypePage.do",e);
            	vo.put("returnCode", 999);
	    		vo.put("saveNum", 0);
	    		vo.put("msg", SysConstant.UPDATE_FAIL_STR);
            }
	    	return vo;
	    	
	    }
	    
	    
	    @ResponseBody
	    @RequestMapping(value="deletetypePage.do")
	    public Object deletetypePage(GoodsTypeQO qo,ModelMap map) throws IOException
	    {
	    	logger.info("deletetypePage.do");
            Map<String,Object> vo = new HashMap<String,Object>();
	    	int a = service.deleteGoodsType(qo);
	    	if(a>0){
	    		vo.put("returnCode", 0);
	    		vo.put("saveNum", a);
	    		vo.put("msg", SysConstant.DELETE_SUCCESS_STR);
	    	}else if(a==-1){
	    		vo.put("returnCode", 999);
	    		vo.put("saveNum", a);
	    		vo.put("msg", "该类别下已经有商品了，不能删除！");
	    	}else{
	    		vo.put("returnCode", 999);
	    		vo.put("saveNum", a);
	    		vo.put("msg", SysConstant.DELETE_FAIL_STR);
	    	}
	    	return vo;
	    	
	    }
	    
	    @RequestMapping(value="toQuerygoods.do")
	    public String toQuerygoods(ModelMap map) throws IOException
	    {
	    	logger.info("toQuerygoods.do");
	    	GoodsTypeQO qo = new GoodsTypeQO();
	    	//qo.setPid("-1");
	    	qo.setCurPage(-1);//不要分页查询
	    	Map<String,Object> vo = service.queryGoodsType(qo);
	    	map.put("list", vo.get("infoContent"));
			return "view/backstage/goods/queryGoods";
	    	
	    }
	    
	    @ResponseBody
		@RequestMapping(value="queryGoods.do")
	    public Object queryGoods(GoodsQO qo)
	    {
	    	logger.info("queryGoods.do  start...");

	    	Map<String,Object> vo = service.queryGoods(qo);
	    	return vo;
	    }
	    
	    @RequestMapping(value="toAddgoods.do")
	    public String toAddgoods(ModelMap map) throws IOException
	    {
	    	logger.info("toAddgoods.do");
	    	User u =  (User)map.get("user");
	    	if(u==null){
	    		return "view/common/noLogin";// 没有登入 直接到登入界面 
	    	}
	    	
	    	
	    	GoodsTypeQO qo = new GoodsTypeQO();
	    	//qo.setPid("-1");
	    	qo.setCurPage(-1);//不要分页查询
	    	Map<String,Object> vo = service.queryGoodsType(qo);
	    	map.put("list", vo.get("infoContent"));
			return "view/backstage/goods/addGood";
	    	
	    }
	    
	    /**
	     * 商品增加
	     * @param qo
	     * @param map
	     * @return
	     * @throws IOException
	     */
	    @ResponseBody
	    @RequestMapping(value="addGoods.do")
	    public Object addGoods(GoodsQO qo,ModelMap map) throws IOException
	    {
		logger.info("addGood.do....");
		Map<String, Object> vo = new HashMap<String, Object>();
		User u = (User) map.get("user");
		if (u != null) {
			qo.setOptUser(u.getAccount());
			try {
				int a = service.saveGoods(qo);
				if (a > 0) {
					vo.put("returnCode", 0);
					vo.put("saveNum", a);
					vo.put("msg", SysConstant.SAVE_SUCCESS_STR);
				} else {
					vo.put("returnCode", 999);
					vo.put("saveNum", a);
					vo.put("msg", SysConstant.SAVE_FAIL_STR);
				}
			} catch (Exception e) {
				logger.error("addGood.do", e);
				vo.put("returnCode", 999);
				vo.put("saveNum", 0);
				vo.put("msg", SysConstant.SAVE_FAIL_STR);
			}
		}else{
			vo.put("returnCode", 999);
			vo.put("saveNum", 0);
			vo.put("msg", SysConstant.NOT_LOGIN);
		}
		 
		     return vo;
	    }
	    
	    /**
	     * 跳转到商品修改界面
	     * @param map
	     * @return
	     * @throws IOException
	     */
	    @RequestMapping(value="toUpdategoods.do")
	    public String toUpdategoods(GoodsQO qo1,ModelMap map) throws IOException
	    {
	    	logger.info("toUpdategoods.do");
	    	User u =  (User)map.get("user");
	    	if(u==null){
	    		return "view/common/noLogin";// 没有登入 直接到登入界面 
	    	}
	    	
	    	
	    	qo1.setCurPage(-1);//不要分页查询
	    	GoodsQO goods  = service.querySingleGoods(qo1);
	    	if(goods!=null){
	    	   map.put("vo", goods);
	    	}
	    	
	    	GoodsTypeQO qo = new GoodsTypeQO();
	    	//qo.setPid("-1");
	    	qo.setCurPage(-1);//不要分页查询
	    	Map<String,Object> vo = service.queryGoodsType(qo);
	    	map.put("list", vo.get("infoContent"));
	    	
	    	GoodsTrait qo2 = new GoodsTrait();
	    	qo2.setGoodsId(qo1.getId());
	    	
	    	List<Map<String,Object>> imgs = service.queryGoodsImg(qo2);
	    	map.put("imgs", imgs);
			return "view/backstage/goods/updateGood";
	    	
	    }
	    
	    /**
	     * 商品信息查看
	     * @param qo1
	     * @param map
	     * @return
	     * @throws IOException
	     */
	    @RequestMapping(value="toViewgoods.do")
	    public String toViewgoods(GoodsQO qo1,ModelMap map) throws IOException
	    {
	    	logger.info("toViewgoods.do");
	    	User u =  (User)map.get("user");
	    	if(u==null){
	    		return "view/common/noLogin";// 没有登入 直接到登入界面 
	    	}
	    	qo1.setCurPage(-1);//不要分页查询
	    	GoodsQO goods  = service.querySingleGoods(qo1);
	    	if(goods!=null){
	    	   map.put("vo", goods);
	    	}
	    	
	    	GoodsTrait qo2 = new GoodsTrait();
	    	qo2.setGoodsId(qo1.getId());
	    	
	    	List<Map<String,Object>> imgs = service.queryGoodsImg(qo2);
	    	map.put("imgs", imgs);
	    	
	    	
			return "view/backstage/goods/viewGood";
	    }
	    
	    
	    @RequestMapping(value="test.do")
	    public String test(GoodsQO qo1,ModelMap map) throws IOException
	    {
	    	logger.info("toViewgoods.do");
	    	User u =  (User)map.get("user");
	    	if(u==null){
	    		return "view/common/noLogin";// 没有登入 直接到登入界面 
	    	}
	    	
	    	
			return "view/common/upload";
	    }
	    
	    @RequestMapping(value="toUpdateGoodsImg.do")
	    public String toUpdateGoodsImg(GoodsTrait qo,ModelMap map) throws IOException
	    {
	    	logger.info("toUpdateGoodsImg.do");
			return "view/backstage/goods/updateGoodsImg";
	    }
	    
	    @ResponseBody
	    @RequestMapping(value="queryGoodsImag.do")
	    public Object queryGoodsImag(GoodsTrait qo) throws IOException
	    {
	    	logger.info("queryGoodsImag.do");
	    	List<Map<String,Object>> vo = service.queryGoodsImg(qo);
	    	
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	map.put("rowTotal", vo.size());
	    	map.put("infoContent", vo);
			return map;
	    }
	    
	    @RequestMapping(value="toViewGoodsTrait.do")
	    public String toViewGoodsTrait(GoodsTrait qo,ModelMap map) throws IOException
	    {
	    	logger.info("toViewGoodsTrait.do");
	    	/*Map<String,Object> vo = service.queryGoodsTrait(qo);
	    	map.put("vo", vo);*/
			return "view/backstage/goods/viewGoodsTrait";
	    }
	    
	    @ResponseBody
	    @RequestMapping(value="updateGoodsTrait.do")
	    public Object updateGoodsTrait(GoodsTrait qo,ModelMap map) throws IOException
	    {
	    	logger.info("updateGoodsTrait.do");
	        
	        
	        List<GoodsTrait> list = new ArrayList<GoodsTrait>();
	    	User u =  (User)map.get("user");
	    	Map<String,Object> vo = new HashMap<String,Object>();
	    	try {
    			 
    			qo.setOptUser(u.getAccount());
    			list.add(qo);
    			
				int a = service.updateGoodsTrait(list);
				if (a > 0) {
					vo.put("returnCode", 0);
					vo.put("saveNum", a);
					vo.put("msg", SysConstant.SAVE_SUCCESS_STR);
				} else {
					vo.put("returnCode", 999);
					vo.put("saveNum", a);
					vo.put("msg", SysConstant.UPDATE_FAIL_STR);
				}
			} catch (Exception e) {
				logger.error("updateGoods.do:", e);
				vo.put("returnCode", 999);
				vo.put("saveNum", 0);
				vo.put("msg", SysConstant.UPDATE_FAIL_STR);
			}
			return vo;
	    }
	    
	     
	    
	    @ResponseBody
	    @RequestMapping(value="updateGoods.do")
	    public Object updateGoods(GoodsQO qo,ModelMap map) throws IOException
	    {
	    	logger.info("updatetypePage.do...");
	    	User u =  (User)map.get("user");
	    	Map<String,Object> vo = new HashMap<String,Object>();
		if (u != null) {
			try {
				qo.setOptUser(u.getAccount());
				int a = service.updateGoods(qo);
				if (a > 0) {
					vo.put("returnCode", 0);
					vo.put("saveNum", a);
					vo.put("msg", SysConstant.SAVE_SUCCESS_STR);
				} else {
					vo.put("returnCode", 999);
					vo.put("saveNum", a);
					vo.put("msg", SysConstant.UPDATE_FAIL_STR);
				}
			} catch (Exception e) {
				logger.error("updateGoods.do:", e);
				vo.put("returnCode", 999);
				vo.put("saveNum", 0);
				vo.put("msg", SysConstant.UPDATE_FAIL_STR);
			}
		} else {
			// 没有登入
			vo.put("returnCode", 999);
			vo.put("saveNum", 0);
			vo.put("msg", SysConstant.NOT_LOGIN);
		}
	    	return vo;
	    	
	    }
	    
	    
	    @ResponseBody
	    @RequestMapping(value="deleteGoods.do")
	    public Object deleteGoods(GoodsQO qo,ModelMap map) throws IOException
	    {
	    	logger.info("deletetypePage.do");
	    	User u =  (User)map.get("user");
	    	Map<String,Object> vo = new HashMap<String,Object>();
	    	if(u!=null){
	        qo.setOptUser(u.getAccount());
	    	int a = service.deleteGoods(qo);
	    	if(a>0){
	    		vo.put("returnCode", 0);
	    		vo.put("saveNum", a);
	    		vo.put("msg", SysConstant.DELETE_SUCCESS_STR);
	    	}else{
	    		vo.put("returnCode", 999);
	    		vo.put("saveNum", a);
	    		vo.put("msg", SysConstant.DELETE_FAIL_STR);
	    	}
	    	}else{
	    		vo.put("returnCode", 999);
	    		vo.put("saveNum", 0);
	    		vo.put("msg", SysConstant.NOT_LOGIN);
	    	}
	    	return vo;
	    	
	    }
}
